package com.dhcc.bpm.modules.sandbox.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.sandbox.entity.TestDictType;
import com.dhcc.bpm.modules.sandbox.service.ITestDictDataService;
import com.dhcc.bpm.modules.sandbox.service.ITestDictTypeService;
import com.dhcc.bpm.modules.sandbox.vo.TestDictDataVO;
import com.dhcc.bpm.modules.sandbox.vo.TestDictTypeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TestDictTypeController
 * @Description 沙箱测试数据 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("bpm-sandbox/testdicttype")
@Api(value = "沙箱测试数据", tags = "沙箱测试数据接口")
public class TestDictTypeController extends BladeController {

	private ITestDictTypeService testDictTypeService;

	private ITestDictDataService testDictDataService;

	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 21:36 2020/2/21 0021
	 * @param testDictType
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.sandbox.entity.TestDictType>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入testDictType")
	@ApiLog("沙箱测试数据详情")
	@Log(title = "沙箱测试数据详情", businessType = BusinessType.SELECT)
	public R<TestDictType> detail(TestDictType testDictType) {
		TestDictType detail = testDictTypeService.getOne(Condition.getQueryWrapper(testDictType));
		return R.data(detail);
	}

	/**
	 * @Description 分页 沙箱测试数据
	 * @Author sjm
	 * @Date 21:37 2020/2/21 0021
	 * @param testDictType
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.entity.TestDictType>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入testDictType")
	@ApiLog("沙箱测试数据")
	@Log(title = "沙箱测试数据", businessType = BusinessType.SELECT)
	public R<IPage<TestDictType>> list(@ApiIgnore @RequestParam Map<String, Object> testDictType, Query query) {
		IPage<TestDictType> pages = testDictTypeService.page(Condition.getPage(query), Condition.getQueryWrapper(testDictType, TestDictType.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 沙箱测试数据
	 * @Author sjm
	 * @Date 21:37 2020/2/21 0021
	 * @param testDictType
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.TestDictTypeVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入testDictType")
	@ApiLog("分页查询沙箱测试数据")
	@Log(title = "沙箱测试数据", businessType = BusinessType.SELECT)
	public R<IPage<TestDictTypeVO>> page(TestDictTypeVO testDictType, Query query) {
		IPage<TestDictTypeVO> pages = testDictTypeService.selectTestDictTypePage(Condition.getPage(query), testDictType);
		return R.data(pages);
	}

	/**
	 * @Description 新增 沙箱测试数据
	 * @Author sjm
	 * @Date 21:37 2020/2/21 0021
	 * @param testDictType
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入testDictType")
	@ApiLog("新增沙箱测试数据")
	@Log(title = "新增沙箱测试数据", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody TestDictType testDictType) {
		return R.status(testDictTypeService.save(testDictType));
	}

	/**
	 * @Description 修改 沙箱测试数据
	 * @Author sjm
	 * @Date 21:37 2020/2/21 0021
	 * @param testDictType
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入testDictType")
	@ApiLog("修改沙箱测试数据")
	@Log(title = "修改沙箱测试数据", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody TestDictType testDictType) {
		return R.status(testDictTypeService.updateById(testDictType));
	}

	/**
	 * @Description 新增或修改 沙箱测试数据
	 * @Author sjm
	 * @Date 21:37 2020/2/21 0021
	 * @param testDictType
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入testDictType")
	@ApiLog("新增或修改沙箱测试数据")
	@Log(title = "新增或修改沙箱测试数据", businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody TestDictType testDictType) {
		return R.status(testDictTypeService.saveOrUpdate(testDictType));
	}

	
	/**
	 * @Description 删除 沙箱测试数据
	 * @Author sjm
	 * @Date 21:38 2020/2/21 0021
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("删除沙箱测试数据")
	@Log(title = "删除沙箱测试数据", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> idList = Func.toLongList(ids);
		for (long id : idList){
			TestDictType detail = testDictTypeService.getById(id);
			List<TestDictDataVO> data = testDictDataService.getByTestType(detail.getTestType());
			if(data.size() > 0){
				return R.fail(201, "存在子数据，暂时无法删除！");
			}
		}
		return R.status(testDictTypeService.deleteLogic(Func.toLongList(ids)));
	}

	
}
